package com.jhj.utils;

import java.io.IOException;
import java.util.*;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonUtil {
	private static Logger logger = Logger.getLogger(JacksonUtil.class);
	private static final ObjectMapper objectMapper;
	static {
		objectMapper = new ObjectMapper();
		// //去掉默认的时间戳格式
		// objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
		// false);
		//
		//
		// //设置为中国上海时区
		// objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		// objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
		// //空值不序列化
		// objectMapper.setSerializationInclusion(Include.NON_NULL);
		// //反序列化时，属性不存在的兼容处理
		// objectMapper.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		// //序列化时，日期的统一格式
		// objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		//
		// objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		// objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
		// false);
		// //单引号处理
		// objectMapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES,
		// true);
		// //无双引号处理
		objectMapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
	}

	public static <T> T toObject(String json, Class<T> clazz) {
		try {
			return objectMapper.readValue(json, clazz);
		} catch (JsonParseException e) {
			logger.error(e.getMessage(), e);
		} catch (JsonMappingException e) {
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public static <T> String toJson(T entity) {
		try {
			return objectMapper.writeValueAsString(entity);
		} catch (JsonGenerationException e) {
			logger.error(e.getMessage(), e);
		} catch (JsonMappingException e) {
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public static <T> T toCollection(String json, TypeReference<T> typeReference) {
		try {
			return objectMapper.readValue(json, typeReference);
		} catch (JsonParseException e) {
			logger.error(e.getMessage(), e);
		} catch (JsonMappingException e) {
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * json string convert to map
	 */
	public static <T> Map<String, Object> json2map(String jsonStr) throws Exception {
		return objectMapper.readValue(jsonStr, Map.class);
	}

	/**
	 * json string convert to map with javaBean
	 */
	public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz) throws Exception {
		Map<String, Map<String, Object>> map = objectMapper.readValue(jsonStr, new TypeReference<Map<String, T>>() {
		});
		Map<String, T> result = new HashMap<String, T>();
		for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
			result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
		}
		return result;
	}

	/**
	 * json array string convert to list with javaBean
	 */
	public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception {
		List<Map<String, Object>> list = objectMapper.readValue(jsonArrayStr, new TypeReference<List<T>>() {
		});
		List<T> result = new ArrayList<T>();
		for (Map<String, Object> map : list) {
			result.add(map2pojo(map, clazz));
		}
		return result;
	}

	/**
	 * map convert to javaBean
	 */
	public static <T> T map2pojo(Map map, Class<T> clazz) {
		return objectMapper.convertValue(map, clazz);
	}

	public static void main(String[] args) throws Exception {
		// String jsonStr =
		// "[{\"asset_id\":22,\"asset_name\":\"笔记本\",\"total\":2},{\"asset_id\":21,\"asset_name\":\"啦啦啦\",\"total\":2},{\"asset_id\":20,\"asset_name\":\"看看\",\"total\":3}]";

		// jsonStr = new String (jsonStr);
		// Map<String, String> result = GsonUtil.GsonToMaps(jsonStr);

		// System.out.println(result.toString());
		// List<HashMap> assets = new ArrayList<HashMap>();
		// try {
		// assets = JacksonUtil.json2list(jsonStr, HashMap.class);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// for (HashMap item : assets) {
		// System.out.println(item.get("asset_id").toString());
		// }
		String json = "{\"status\":0,\"msg\":\"ok\",\"data\":{\"id\":27488,\"am_id\":0,\"user_id\":17985,\"mobile\":\"1383838438\",\"city_id\":0,\"org_id\":0,\"addr_id\":19654,\"order_addr\":\"北京市海淀区大钟寺华杰大厦B座215海淀区大钟寺华杰大厦\",\"order_no\":\"935826937102729216\",\"order_type\":0,\"service_type\":68,\"service_content\":\"基础保洁\",\"service_date\":1512001812,\"service_hour\":3.0,\"staff_nums\":1,\"order_status\":1,\"order_rate\":0,\"order_rate_content\":\"\",\"order_from\":3,\"order_op_from\":130,\"remarks\":\"来之前请电话确认\",\"remarks_confirm\":\"\",\"add_time\":1511953503,\"update_time\":1511953503,\"order_done_time\":0,\"remarks_bussiness_confirm\":\"\",\"period_order_id\":0,\"del_flag\":0,\"validate_code\":0}}";
		Map<String, Object> resultMap = JacksonUtil.json2map(json);
		System.out.println(resultMap);

	}

}